package com.ruoyi.crm.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.crm.domain.AssignRecord;
import com.ruoyi.crm.domain.Business;
import com.ruoyi.crm.domain.vo.ClueTrackVo;
import com.ruoyi.crm.domain.vo.FalseClueVo;
import com.ruoyi.crm.service.IActivityService;
import com.ruoyi.crm.service.IAssignRecordService;
import com.ruoyi.crm.service.IBusinessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.mapper.ClueMapper;
import com.ruoyi.crm.domain.Clue;
import com.ruoyi.crm.service.IClueService;

import javax.annotation.Resource;

/**
 * 线索Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-29
 */
@Service
public class ClueServiceImpl implements IClueService {
    @Resource
    private ClueMapper clueMapper;
    @Resource
    private   IAssignRecordService iAssignRecordService;
    @Resource
    private IBusinessService iBusinessService;
    /**
     * 查询线索
     *
     * @param id 线索主键
     * @return 线索
     */
    @Override
    public Clue selectClueById(Long id) {
        return clueMapper.selectClueById(id);
    }

    /**
     * 查询线索列表
     *
     * @param clue 线索
     * @return 线索
     */
    @Override
    public List<Clue> selectClueList(Clue clue) {
        return clueMapper.selectClueList(clue);
    }

    /**
     * 新增线索
     *
     * @param clue 线索
     * @return 结果
     */
    @Override
    public int insertClue(Clue clue) {
        clue.setCreateTime(DateUtils.getNowDate());
        return clueMapper.insertClue(clue);
    }

    @Override
    public int insertClues(List<Clue> clue) {
        int sum = 0;
        for (Clue clue1 : clue) {
            clue1.setChannel(DictUtils.getDictValue("clues_item", clue1.getChannel()));
            clue1.setSex(DictUtils.getDictValue("sys_user_sex", clue1.getSex()));
            clue1.setLevel(DictUtils.getDictValue("clues_level", clue1.getLevel()));
            clue1.setStatus("0");
            clue1.setSubject(DictUtils.getDictValue("course_subject", clue1.getSubject()));
            sum += this.insertClue(clue1);
        }
        return sum;
    }

    /**
     * 修改线索
     *
     * @param clue 线索
     * @return 结果
     */
    @Override
    public int updateClue(Clue clue) {
        clue.setUpdateTime(DateUtils.getNowDate());
        return clueMapper.updateClue(clue);
    }

    /**
     * 批量删除线索
     *
     * @param ids 需要删除的线索主键
     * @return 结果
     */
    @Override
    public int deleteClueByIds(Long[] ids) {
        return clueMapper.deleteClueByIds(ids);
    }

    /**
     * 删除线索信息
     *
     * @param id 线索主键
     * @return 结果
     */
    @Override
    public int deleteClueById(Long id) {
        return clueMapper.deleteClueById(id);
    }

    //捞取
    @Override
    public int restClue(Long id) {
        AssignRecord assignRecord = new AssignRecord();
        assignRecord.setType("0");
        assignRecord.setAssignId(id);
        assignRecord.setUserId(SecurityUtils.getUserId());
        assignRecord.setUserName(SecurityUtils.getUsername());
        return iAssignRecordService.insertAssignRecord(assignRecord);
    }

    @Override
    public int falesClue(FalseClueVo falseClueVo) {
        Clue clue =this.selectClueById(falseClueVo.getId());
        if(clue.getFalseCount()>=3){
            throw new RuntimeException("伪线索次数超限");
        }
        clue.setFalseCount(clue.getFalseCount()+1);
        clue.setStatus("4");
        return this.updateClue(clue);
    }

    @Override
    public int formbusiness(ClueTrackVo clueTrackVo) {
        Clue clue = this.selectClueById(clueTrackVo.getId());
        clue.setStatus("5");
        this.updateClue(clue);

        Business business = new Business();
        BeanUtils.copyProperties(clue,business);
        business.setStatus("0");
        iBusinessService.insertBusiness(business);

        AssignRecord assignRecord = new AssignRecord();
        assignRecord.setType("1");
        assignRecord.setUserName(SecurityUtils.getUsername());
        assignRecord.setUserId(SecurityUtils.getUserId());
        assignRecord.setAssignId(business.getId());
        return iAssignRecordService.insertAssignRecord(assignRecord);
    }
}
